//
//  OpenSendViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/25.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "OpenSendViewController.h"
#import "SocketRequest.h"
#import "Singleton.h"
#import "TSHeader.h"
#import "CustomAlertView.h"

@interface OpenSendViewController ()<SocketRequestDelegate> {
    SocketRequest *_socketRequest;
}

@end

@implementation OpenSendViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"上门件";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/documents/door_express_illustration.html",[Singleton shareInstance].cityModel.manageSystemUrl]]];
    [_webView loadRequest:request];
}

- (void)openSendSocket {
    [ToastActivity makeActivity:@"提交中..." InView:self.view];
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STSC"]];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    [ToastActivity dismissInView:self.view];
    if ([code isEqualToString:SuccessSocket]) {
        CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:nil message:@"尊敬的站主，您的申请已提交，我们将尽快安排人员与您联系。" delegate:nil cancelButtonTitle:nil sureButtonTitle:@"我知道了"];
        [alert show];
    } else {
        CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil sureButtonTitle:@"我知道了"];
        [alert show];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}

#pragma mark NibAction
- (IBAction)openSendAction:(id)sender {
    [self openSendSocket];
}

- (void)dealloc {
    _socketRequest = nil;
}

@end
